<template>
  <view class="hot-products" :style="{'background': item.bgColor, 'margin-left': item.bgMargin + 'px', 'margin-right': item.bgMargin + 'px', 'border-radius': item.bgStyle == 'circle' ? '10px' : '0px'}">
    <view class="section-header">
      <text class="section-title">热销商品</text>
      <view class="more-btn" @click="handleMoreClick">
        <text>更多</text><uni-icons type="right" size="14"></uni-icons>
      </view>
    </view>
    
    <view class="product-grid" :class="{'single-column': item.lineNum == 1, 'double-column': item.lineNum == 2}">
      <view
        v-for="product in products"
        :key="product.id"
        class="product-card"
        :class="{'single-product': item.lineNum == 1, 'double-product': item.lineNum == 2}"
        @click="handleProductClick(product)"
      >
        <image :src="product.slider_image" :alt="product.name" class="product-image" />
        <view class="product-info">
          <text class="product-name" v-if="item.show_title == 1">{{ product.name }}</text>
          <view class="product-price">
            <view class="current-price" v-if="item.show_price == 1">
              <text class="currency-symbol">¥</text>
              <text class="price-value">{{ product.price }}</text>
            </view>
            <text class="original-price" v-if="item.show_origin_price == 1">¥{{ product.original_price }}</text>
          </view>
          <text class="product-sales">已售 {{ product.sales }}</text>
        </view>
        <!-- 右下角plus按钮 -->
        <view class="product-plus-btn" @click.stop="handleAddToCart(product)">
          <uni-icons type="plusempty" size="16" color="#fff"></uni-icons>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'HotProducts',
  props: {
    item: {
      type: Object,
      default: {}
    }
  },
  data() {
	  return {
		  products: []
	  }
  },
  mounted() {
  	this.getGoodList()
  },
  methods: {
    // 处理商品点击
    handleProductClick(product) {
      uni.navigateTo({
      	url: '/pages/product/product?id=' + product.id
      })
    },
    // 处理加入购物车
    handleAddToCart(product) {
      uni.navigateTo({
      	url: '/pages/product/product?id=' + product.id
      })
    },
    // 处理更多点击
    handleMoreClick() {
      uni.switchTab({
      	url: '/pages/category/category'
      })
    },
	// 获取商品列表
	async getGoodList() {
		let res = await this.$api.home.getGoodList.get({num: this.item.num, sortType: this.item.sortType, cateId: this.item.cate_id})
		this.products = res.data
	}
  }
}
</script>

<style scoped lang="scss">
.hot-products {
  padding: 16px 0 0 0;
  border-radius: 8px;
}

.dark-theme .hot-products {
  background-color: #2a2a2a;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.section-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 12px;
  color: #333;
  padding-top: 20rpx;
}

.dark-theme .section-title {
  color: #fff;
}

.more-btn {
  color: #999;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
}

.product-grid {
  display: grid;
  gap: 12px;
  
  /* 确保内容正确包裹 */
  overflow: hidden;
}

/* 一行一个商品的布局 */
.product-grid.single-column {
  grid-template-columns: 1fr;
}

/* 一行两个商品的布局（默认） */
.product-grid.double-column {
  grid-template-columns: repeat(2, 1fr);
}

.product-card {
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  position: relative;
  background-color: #fff;
}

/* 一行一个商品的样式 */
.product-card.single-product {
  display: flex;
  height: 120px;
}

.product-card.single-product .product-image {
  width: 120px;
  height: 120px;
  flex-shrink: 0;
}

.product-card.single-product .product-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.product-card.single-product .product-name {
  -webkit-line-clamp: 1;
}

.product-card.single-product .product-plus-btn {
  right: 12px;
  bottom: 12px;
}

/* 一行两个商品的样式 */
.product-card.double-product {
  /* 保持原有样式 */
}

.dark-theme .product-card {
  background-color: #333;
}

/* 商品卡片右下角加号按钮 */
.product-plus-btn {
  position: absolute;
  right: 8px;
  bottom: 8px;
  width: 22px;
  height: 22px;
  background: linear-gradient(90deg, #e93323 0%, #FF7931 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.product-plus-btn:hover {
  transform: scale(1.1);
  box-shadow: 0 4px 12px rgba(233, 57, 57, 0.4);
}

.product-plus-btn:active {
  transform: scale(0.95);
}

.product-image {
  width: 100%;
  height: 160px;
  object-fit: cover;
}

/* 一行两个商品布局的图片样式 */
.double-product .product-image {
  width: 100%;
  height: 160px;
  object-fit: cover;
}

.product-info {
  padding: 12px;
}

.product-name {
  font-size: 14px;
  margin-bottom: 8px;
  color: #333;
  font-family: 'PingFang SC', sans-serif;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.dark-theme .product-name {
  color: #fff;
}

.product-price {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.product-price .current-price {
  display: flex;
  align-items: baseline;
}

.currency-symbol {
  font-size: 12px;
  font-weight: normal;
  color: #e93323;
  margin-right: 2px;
}

.price-value {
  font-size: 16px;
  font-weight: bold;
  color: #e93323;
}

.original-price {
  font-size: 12px;
  color: #999;
  text-decoration: line-through;
}

.product-sales {
  font-size: 12px;
  color: #999;
}
</style>